package com.qiangesoft.blog.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.blog.common.mvc.PageQuery;
import com.qiangesoft.blog.common.mvc.PageResultVO;
import com.qiangesoft.blog.common.utils.DateUtils;
import com.qiangesoft.blog.core.entity.BlogTool;
import com.qiangesoft.blog.core.mapper.BlogToolMapper;
import com.qiangesoft.blog.core.pojo.query.SourceQueryDTO;
import com.qiangesoft.blog.core.pojo.vo.ToolInfoVO;
import com.qiangesoft.blog.core.service.IBlogToolService;
import com.qiangesoft.blog.file.model.FileInfoVo;
import com.qiangesoft.blog.file.service.ISysFileInfoService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工具 服务实现类
 * </p>
 *
 * @author qiange
 * @since 2023-06-16
 */
@Service
@RequiredArgsConstructor
public class BlogToolServiceImpl extends ServiceImpl<BlogToolMapper, BlogTool> implements IBlogToolService {

    private final ISysFileInfoService sysFileInfoService;

    @Override
    public PageResultVO<ToolInfoVO> listTool(PageQuery pageQuery) {
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        PageResultVO<ToolInfoVO> pageResult = new PageResultVO<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);

        LambdaQueryWrapper<BlogTool> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogTool::getIsPublish, true)
                .orderByDesc(BlogTool::getIsStick);

        Page<BlogTool> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        List<BlogTool> records = page.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return pageResult;
        }

        List<Long> fileIds = records.stream().map(BlogTool::getToolFileId).collect(Collectors.toList());
        Map<Long, FileInfoVo> fileInfoVoMap = sysFileInfoService.getFileInfoByIds(fileIds).stream().collect(Collectors.toMap(FileInfoVo::getFileId, fileInfoVo -> fileInfoVo));

        List<ToolInfoVO> voList = new ArrayList<>();
        for (BlogTool record : records) {
            ToolInfoVO vo = new ToolInfoVO();
            vo.setId(record.getId());
            vo.setTitle(record.getTitle());
            vo.setAvatar(record.getAvatar());
            vo.setSummary(record.getSummary());
            vo.setContent(record.getContent());
            vo.setContentMd(record.getContentMd());
            vo.setIsStick(record.getIsStick());
            vo.setCreateTime(DateUtils.localDateTimeToDate(record.getCreateTime()));
            vo.setQuantity(record.getQuantity());
            vo.setDownloadNum(record.getDownloadNum());
            vo.setFileInfo(fileInfoVoMap.get(record.getToolFileId()));
            voList.add(vo);
        }
        pageResult.setTotal(page.getTotal());
        pageResult.setPages(page.getPages());
        pageResult.setResults(voList);
        return pageResult;
    }

    @Override
    public PageResultVO<BlogTool> pageTool(PageQuery pageQuery, SourceQueryDTO sourceQuery) {
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        PageResultVO<BlogTool> pageResult = new PageResultVO<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);

        LambdaQueryWrapper<BlogTool> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogTool::getIsPublish, true)
                .like(StringUtils.isNotBlank(sourceQuery.getTitle()), BlogTool::getTitle, sourceQuery.getTitle())
                .orderByDesc(BlogTool::getIsStick);
        Page<BlogTool> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        pageResult.setTotal(page.getTotal());
        pageResult.setPages(page.getPages());
        pageResult.setResults(page.getRecords());
        return pageResult;
    }

    @Override
    public BlogTool getToolById(Long id) {
        BlogTool blogTool = baseMapper.selectById(id);
        FileInfoVo info = sysFileInfoService.getFileInfoById(blogTool.getToolFileId());
        blogTool.setToolFileInfo(info);
        return blogTool;
    }

    @Override
    public void insertTool(BlogTool tool) {
        baseMapper.insert(tool);
    }

    @Override
    public void updateTool(BlogTool tool) {
        baseMapper.updateById(tool);
    }

    @Override
    public void deleteTool(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteBatchTool(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public void putTopTool(BlogTool tool) {
        baseMapper.updateById(tool);
    }

    @Override
    public void publishAndShelf(BlogTool tool) {
        baseMapper.updateById(tool);
    }
}
